<?php

  if (isset($_POST['submit0'])) { // If password entered...
    $word = $_POST['password']; // Variable set as entered password
    setcookie('textadmin', $word, time() + 31536000); // Cookie is set
  } else { // If password form filled in previously...
    if (isset($_COOKIE['textadmin'])) { // Check for cookie
      $word = $_COOKIE['textadmin']; // Variable set as cookie
    }
  }

  // Retrieve the file contents
  $page = $_GET['page'];
  $filename = "http://cit3.ldl.swin.edu.au/~mascot/cms/textfiles/$page.txt";
  if (file_exists($filename)) {
    // Get the contents
    $file_contents = file_get_contents($filename);
    $file_contents = htmlspecialchars($file_contents);
  }

  // Apend
  if (isset($_POST['submit1'])) { // Handle the form.
    $content = $_POST['addtext'];
    $content = stripslashes($content);
    $page = $_POST['page'];
    $filename = 'http://cit3.ldl.swin.edu.au/~mascot/cms/textfiles/' . $page . '.txt';
    $fp = fopen($filename, "a") or die("Couldn't open $filename.");
    fwrite($fp, "\n\n$content");
    fclose($fp);
    $response = "The content was added.";
  }

  // Over-write
  if (isset($_POST['submit2'])) { // Handle the form.
    $content = $_POST['addtext'];
    $content = stripslashes($content);
    $page = $_POST['page'];
    $filename = 'http://cit3.ldl.swin.edu.au/~mascot/cms/textfiles/' . $page . '.txt';
    $fp = fopen($filename, "w") or die("Couldn't open $filename.");
    fwrite($fp, "\n$content");
    fclose($fp);
    $response = "The content was overwritten.";
  }

?>

<?php $thisPage = "write"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Write page content</title>
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body>

<div id="wrap">

<?php

// Start password routine
  include_once('password.php');
  if ($word != $le_password) {

?>

<!-- password form //-->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>
<b>Enter password:</b>
<br />
<input type="password" name="password" size="25" maxlength="25" />
<br />
<input type="submit" name="submit0" value="Submit" />
</p>
</form>
<!-- password form_eof //-->

<?php

    // Routine to display password entry error message.
    if (isset($_POST['submit0'])) {
      if ($_POST['password'] != $le_password) {
        echo '<p">Incorrect password!</p>';
      }
    }

  } else {
// End password routine

?>

<?php include_once('http://cit3.ldl.swin.edu.au/~mascot/cms/navigation.php'); ?>

<?php echo 'Page = ' . $page . '<br />'; ?>
Path to images = /cms/images/myimage.jpg
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" accept-charset="UTF-8">
<textarea class="textarea" name="addtext" cols="70" rows="25"><?php if ((isset($_POST['submit1'])) || (isset($_POST['submit2']))) { echo htmlspecialchars(stripslashes($_POST['addtext'])); } else { echo $file_contents; } ?></textarea><br />
<input type="hidden" name="page" value="<?php echo $page; ?>" />
<input type="submit" name="submit1" value="Append" /> <input type="submit" name="submit2" value="Overwrite" />
</form>

<?php echo $response; ?>

<?php

  if ($page == "index") {
    echo '<a href="http://cit3.ldl.swin.edu.au/~mascot/">View &raquo;</a>';
  } else {
    echo '<a href="http://cit3.ldl.swin.edu.au/~mascot/cms/' . $page . '">View &raquo;</a>';
  }

?>

<?php

  } // Required for password routine

?>

</div>

</body>
</html>
